/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.system.application.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.system.application.RouterDatabaseApplication;
import com.foreveross.bsl.system.application.vo.RouterDatabaseVO;
import com.foreveross.bsl.system.model.domain.RouterDatabase;

/**
 * @description
 *
 * @author rening
 * @version v1.0
 *
 * @2013-6-7
 *
 */
@Named("routerDatabaseApplication")
public class RouterDatabaseApplicationImpl implements RouterDatabaseApplication {

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public RouterDatabaseVO saveEntity(RouterDatabaseVO entity) {
		RouterDatabase routerDatabase = new RouterDatabase();
		BeanHelper.copyProperties(routerDatabase, entity);
		routerDatabase.save();
		entity.setId(routerDatabase.getId());
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public RouterDatabaseVO updateEntity(RouterDatabaseVO entity) {
		RouterDatabase routerDatabase = new RouterDatabase();
		BeanHelper.copyProperties(routerDatabase, entity);
		routerDatabase.save();
		BeanHelper.copyProperties(entity, routerDatabase);
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(RouterDatabaseVO entity) {
		RouterDatabase routerDatabase = new RouterDatabase();
		BeanHelper.copyProperties(routerDatabase, entity);
		routerDatabase.delete();
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public RouterDatabaseVO getEntity(String id) {
		RouterDatabase routerDatabase = new RouterDatabase();
		routerDatabase = (RouterDatabase)routerDatabase.findOne(id);
		RouterDatabaseVO vo = new RouterDatabaseVO();
		BeanHelper.copyProperties(vo, routerDatabase);
		return vo;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<RouterDatabaseVO> findAll() {
		RouterDatabase routerDatabase = new RouterDatabase();
		Iterable iterable = routerDatabase.findAll();
		if(iterable != null){
			List<RouterDatabaseVO> result = new ArrayList<RouterDatabaseVO>();
			RouterDatabaseVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				RouterDatabase obj = (RouterDatabase) iterator.next();

				vo = new RouterDatabaseVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable(org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<RouterDatabaseVO> findByPageable(Pageable pageable) {
		RouterDatabase routerDatabase = new RouterDatabase();
		Page page = routerDatabase.findAll(pageable);
		if(page != null && page.getContent() != null){
			List<RouterDatabaseVO> result = new ArrayList<RouterDatabaseVO>();
			RouterDatabaseVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				RouterDatabase obj = (RouterDatabase) iterator.next();

				vo = new RouterDatabaseVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<RouterDatabaseVO> pageVO = new PageImpl<RouterDatabaseVO>(result, pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}

}
